
//
// 101018 - AH - Created.
// 101123 - AH - Allows resources not having tags.
// 101127 - AH - Tags not longer hold by Set<String> by resource but Tags 
//               object.
// 101205 - AH - Renamed from MineMatch.
// 101208 - AH - Moved from org.soekrat.engine.wheel
// 110113 - AH - Made package scope only.
// 

package org.soekrat.engine.retrieval.mine;

import java.util.HashSet;
import java.util.Set;

import org.soekrat.AttributeObject;
import org.soekrat.engine.mine.Tags;
import org.soekrat.engine.visitor.Input;

/**
 * <p>
 *   Represents a match done by keywords on a resource in the mine.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
final class MineScanDetail 
{
  private final AttributeObject m_resource;
  
  private final boolean m_nameMatched;
  
  private final Set<String> m_tagsMatched=new HashSet<String>();
  
  private final Set<String> m_keywords=new HashSet<String>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param r        {@link #getResource()}.
   * @param keywords {@link #getKeywordsMatched()}.
   */
  MineScanDetail(AttributeObject r, Input keywords)
  {
    String name=r.getName();
    if (name==null)
    {
      throw new IllegalArgumentException("r missing name");
    }
    
    if (keywords.contains(name))
    {
      m_nameMatched=true;
      m_keywords.add(name);
    }
    else
    {
      m_nameMatched=false;
    }
    
    Tags tags=(Tags)r.getAttribute("tags");
    if (tags!=null && !tags.isEmpty())
    {  
      for (String k : keywords) 
      {
        if (tags.contains(k)) 
        {
          m_tagsMatched.add(k);
          m_keywords.add(k);
        }
      }
    }
    
    m_resource=r;
  }
  
  @Override
  public String toString() 
  {
    StringBuilder sb=new StringBuilder();
    
    sb.append("nameMatched=").append(m_nameMatched).append(",tagsMatched=")
      .append(m_tagsMatched).append(",resource=").append(m_resource);
    return sb.toString();
  }
  
  /**
   * <p>
   *   Gets 
   *   {@link AttributeObject} that is the resource value.
   * </p>
   * @return Resource.
   */
  AttributeObject getResource(){ return m_resource; }
  
  /**
   * <p>
   *   Gets number of keywords that matched tags and / or name.
   * </p>
   * @return Count.
   */
  int getNumberOfKeywordsThatMatched(){ return m_keywords.size(); }
  
  /**
   * <p>
   *   Gets keywords that matched tags and / or name.
   * </p>
   * @return Keywords.
   */
  String[] geKeywordsThatMatched(){ return m_keywords.toArray(E_STRING_A); }
  
  /**
   * <p>
   *   Tells if resource name is matched.
   * </p>
   * @return {@code true} if is, {@code false} if is not.
   */
  boolean isNameMatched(){ return m_nameMatched; }
  
  /**
   * <p>
   *   Tells if a tag was matched.
   * </p>
   * @return {@code true} if was, {@code false} if was not.
   */
  final boolean isTagMatched(){ return !m_tagsMatched.isEmpty(); }
  
  /**
   * <p>
   *   Gets number tags matched.
   * </p>
   * @return Count.
   */
  int getNumberOfTagsMatched(){ return m_tagsMatched.size(); }
 
  // Used to retrieve String arrays from String collections, java idiom.
  private static final String[] E_STRING_A=new String[0];
  
  /**
   * <p>
   *   The empty array shared.
   * </p>
   */
  static final MineScanDetail[] E_ARRAY=new MineScanDetail[0];
  
}
